<template>
  <div class="login_bg">
    <!-- 头部标题 -->
    <div class="login_title">
      <!-- <img class="logo_bg" src="../../assets/img/logo.png" alt="" /> -->
      <div>
        <h1>安徽能通MES系统</h1>
      </div>
    </div>
    <!-- 表单部分 -->
    <div class="logo">
      <div class="title">系统登录</div>

      <div class="inputbox">
        <img class="username" src="@/static/login/icon_username.png" alt="" />
        <input
          type="text"
          class=""
          v-model="form.username"
          placeholder="请输入用户名"
        />
      </div>

      <div class="inputbox">
        <img class="password" src="@/static/login/icon_password.png" alt="" />

        <input
          type="password"
          class=""
          v-model="form.password"
          placeholder="请输入密码"
        />
      </div>

      <div class="fx_box">
        <div class="inputboxs">
          <img
            class="verificationcode"
            src="@/static/login/icon_verificationcode.png"
            alt=""
          />
          <input
            type="text"
            class=""
            v-model="form.codes"
            maxlength="4"
            placeholder="请输入验证码"
          />
        </div>
        <img
          class="code"
          @click="GetValidateCodebtn"
          :src="'data:image/png;base64,' + code"
          alt=""
        />
      </div>

      <a-checkbox
        class="passwords"
        style="background: transparent"
        v-model:checked="checkAll"
        >记住密码</a-checkbox
      >
      <div class="submit" @click="register">登录</div>
    </div>
    <!-- 尾部 -->

    <div class="versions">版本 1.0.0.1 / 版权所有 © 能通 2021-2025</div>
  </div>
</template>
  
  <script setup>
import { message } from "ant-design-vue";
import { onMounted, ref, reactive } from "vue";
import { menuStore } from "@/store/menu";
import login from "@/api/loginApi";
import { viewTagsStore } from "@/store";

//import { userStore } from "@/stores/index";
//import dbUtils from "../../libs/util.strotage";
/* import {
  GetVersion,
  GetValidateCode,
  UserLogin,
} from "../../api/modules/api.users_opt_logs"; */
import { useRouter } from "vue-router";
const vStore = viewTagsStore();
const useMenuStore = menuStore();
let router = useRouter();
let code = ref("");
let versions = ref("");
let form = reactive({
  codes: "",
  username: "",
  password: "",
});
let checkAll = ref(false);

//const userInfo = userStore();
onMounted(() => {
  form.username = localStorage.getItem("username");
  form.password = localStorage.getItem("password");
  localStorage.removeItem("userInfo");
  localStorage.removeItem("UUID");
  localStorage.removeItem("MgrModules");
  GetValidateCodebtn();
  vStore.$patch({
    viewTags: [
      {
        title: "首页",
        key: "/index",
      },
    ],
  });
});
// 更换验证码
function verificationCode() {
  //GetValidateCodebtn();
}
// 验证码
function GetValidateCodebtn() {
  login.getValidateCodeStr().then((res) => {
    code.value = res.data;
  });
}

// 记住密码开关
function onCheckAllChange() {
  if (checkAll.value) {
    localStorage.setItem("username", form.username);
    localStorage.setItem("password", form.password);
  } else {
    localStorage.removeItem("username");
    localStorage.removeItem("password");
  }
}

// 登录
function register() {
  /*     router.push("/index"); */
  var inputString = form.password.toString();
  // 使用MD5算法对字符串进行哈希运算
  const md5Hash = CryptoJS.MD5(inputString).toString();

  login
    .login({
      LoginType: 1,
      UserName: form.username,
      UserPwd: md5Hash,
      //VerifyCode: "testuuid",
      VerifyCode: form.codes,
    })
    .then((res) => {

      if (res.Result) {
        localStorage.clear();
        message.success("登录成功");
        onCheckAllChange();
        // 存储用户信息
        localStorage.setItem(
          "userInfo",
          JSON.stringify({
            UserName: res.Data.UserName,
            RoleName: res.Data.RoleName,
          })
        );
        // 存储token
        localStorage.setItem("UUID", res.Data.UUID);
        // 存储菜单信息
        localStorage.setItem("MgrModules", JSON.stringify(res.Data.MgrModules));

        // userInfo.UserName = res.Data.UserName;
        router.push("/index");
        useMenuStore.$patch({
          menuList: res.Data.MgrModules,
        });
        useMenuStore.SETMENULIST();
      } else {
        message.error(res.ResultMsg);
        //localStorage.setItem("UUID", res.Data.UUID);
        GetValidateCodebtn();
        //router.push("/login");
      }

      //
    })
    .catch((err) => {
      console.log(err, "erer");
      GetValidateCodebtn();
    });
}
</script>
  
  <style lang="scss" scoped>
// 屏幕大于667

  .login_bg {
    width: 100vw;
    min-height: 100vh;
    background: url("@/static/login/pic_bj.png") no-repeat;
    background-size: contain 100%;

    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;

    & > * {
      width: 100%;
    }
    .login_title {
      display: flex;
      justify-content: center;
      align-items: flex-end;
      overflow: hidden;
      margin-bottom: 20px;

      .logo_bg {
        width: 0.94rem;
        height: 0.54rem;
        margin-bottom: 0.01rem;
      }
      & > div {
       
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        align-items: center;

        h1,
        h2 {
          width: 30px;
          white-space: nowrap;
          text-align: center;
          color: #d9f0ff;
        }

        h1 {
          width: 100%;
          font-size: 40px;

          margin-bottom: 0;
          text-align-last: justify;
        }

        h2 {
          font-size: 0.1rem;
          width: 100%;
          text-align: center;
          margin-bottom: 0;
        }
      }
    }
  }
  .logo {
    min-height: 0.1rem;
    width: 34%;
    background: url("@/static/login/pic_bj02.png") no-repeat;
    background-size: 100% 100%;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    padding: 0.3rem;
    box-sizing: border-box;

    & > * {
      width: 100%;
    }
    .title {
      font-size: 24px;
      color: #5cb7fe;
      margin-bottom: 10px;
      text-align: center;
    }
    .inputbox {
      width: 100%;
      height: 60px;
      background: url("@/static/login/pic_bj10.png") no-repeat;
      background-size: 100% 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 0.1rem;
    }
    .fx_box {
      display: flex;
      justify-content: space-between;
      align-items: flex-end;
      overflow: hidden;
      height: 60px;
      background: url("@/static/login/pic_bj10.png") no-repeat;
      background-size: 100% 100%;
      overflow: hidden;

      & > * {
        flex-shrink: 0;
      }

      .inputboxs {
        width: 64%;
        height: 100%;

        display: flex;
        justify-content: space-between;
        align-items: center;

        & > .inputbox {
          width: 10px;
        }
      }
      .code {
        height: 88%;
        cursor: pointer;
      }
    }
    input {
      position: relative;
      top: 3px;
      background: none;
      border: none;
      padding: 0;
      margin: 0;
      outline: none;
      color: #fff;
      font-size: 18px;
  
      flex: 1;
      margin-left: 20px;

      &::placeholder {
        color: #1a6ba3;
        font-size: 18px;
      }

      &:focus {
        outline: none;
      }
    }

    .passwords {
      font-size: 18px;
      color: #1a6ba3;
    }

    .username {
      width: 27px;
      height: 25px;
      margin-left: 16px;
      margin-top: 5px;
    }

    .password {
      width: 23px;
      height: 29px;
      margin-left: 16px;
      margin-top: 5px;
      font-size: 18px;
    }

    .verificationcode {
      width: 23px;
      height: 27px;
      margin-left: 16px;
      margin-top: 5px;
    }
    .submit {
      cursor: pointer;
      width: 100%;
      height: 0.4rem;
      line-height: 0.4rem;
      margin-top: 0.2rem;
      background: url("@/static/login/submit.png") no-repeat;
      background-size: 100% 100%;
      font-size: 18px;
      text-align: center;
      color: #fff;
    }
  }
  .versions {
    font-size: 14px;
    text-align: center;
    margin-top: 20px;
    color: #5cb7fe;
  }


</style>
  